package com.thesis.object;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * ListUsers singleton class
 * 
 * save all users in list users
 * 
 * @author NgocTuyen
 *
 */
public class ListUsers {
	
	//instance variable
	private static ListUsers instance = new ListUsers();

	private ArrayList<User> listUsers;
	
	/**
	 * Constructor function
	 * 
	 * Constructor
	 * 
	 * @access private 
	 * 
	 * @param null
	 * 
	 * @return null
	 */
	private ListUsers(){
		listUsers = new ArrayList<User>();
	}
	
	/**
	 * getInstance static function
	 * 
	 * @access public
	 * 
	 * @param null
	 * 
	 * @return ListUsers
	 *  + instance object
	 */
	public static ListUsers getInstance(){
		if( instance == null )  
		{  
			     instance = new ListUsers();
		}  

		return instance;
	}
	
	/**
	 * addUserByJson function
	 * 
	 * add all users into list users
	 * 
	 * @access public
	 * 
	 * @param listJson list of all users
	 * 
	 * @return void
	 */
	public void addUserByJson(JSONArray listJson){
		for (int i = 0; i < listJson.length(); i++) {
			try {
				addUser(listJson.getJSONObject(i));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * addUser function
	 * 
	 * add a user in to list users
	 * 
	 * @access private
	 *  
	 * @param jsonObj json of one user
	 * 
	 * @return void
	 */
	private void addUser(JSONObject jsonObj){
		try {

			User user = new User(jsonObj.getInt("_id"),
					jsonObj.getString("username"),
					jsonObj.getString("first_name"),
					jsonObj.getString("last_name"),
					jsonObj.getString("date_of_birth"),
					jsonObj.getString("address"),
					jsonObj.getString("phone_number"),
					jsonObj.getString("image"), 
					jsonObj.getInt("manager_id"),
					jsonObj.getInt("role"));
//			if (user.getRole() == 1){
//				
//			}
//			else if (user.getRole() == 2){
//				user = new User(jsonObj.getInt("_id"),
//						jsonObj.getString("username"),
//						jsonObj.getString("first_name"),
//						jsonObj.getString("last_name"),
//						jsonObj.getString("date_of_birth"),
//						jsonObj.getString("address"),
//						jsonObj.getString("phone_number"),
//						jsonObj.getString("image"), 
//						jsonObj.getInt("_id"),  //manager_id not null to convert int Json
//						jsonObj.getInt("role"));
//				listUsers.add(user);
//			}
//			else {
//				
//			}
//			
			listUsers.add(user);
				
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * getListUser function
	 * 
	 * get list all users in restaurant
	 * 
	 * @access public 
	 * 
	 * @param null
	 * 
	 * @return ArrayList<User>
	 *  + list of all users
	 */
	public ArrayList<User> getListUser(){
		return listUsers;
	}
	
	/**
	 * deleteUser function
	 * 
	 * delete user in listUsers
	 * 
	 * @access public
	 * 
	 * @param id: id of user
	 * 
	 * @return void
	 */
	public void deleteUser(int id){
		for (User user : listUsers) {
			if (user.getId() == id) {
				listUsers.remove(user);
				break;
			}
		}
	}
	
	/**
	 * editUser function
	 * 
	 * edit user of detail
	 * 
	 * @access public
	 * 
	 * @param jsonObj: json of detail object
	 * 
	 * @return void
	 */
	public void editUser(JSONObject jsonObj){
		int id = 0;
		String username = null;
		String firstName = null;
		String lastName = null;
		String dateOfBirth = null;
		String address = null;
		String phoneNumber = null;
		String image = null;
		int managerId = 0;
		int role = 0;
		try {
			id = jsonObj.getInt("_id");
			username = jsonObj.getString("username");
			firstName = jsonObj.getString("first_name");
			lastName = jsonObj.getString("last_name");
			dateOfBirth = jsonObj.getString("date_of_birth");
			address = jsonObj.getString("address");
			phoneNumber = jsonObj.getString("phone_number");
			image = jsonObj.getString("image");
			managerId = jsonObj.getInt("manager_id");
			role = jsonObj.getInt("role");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (User user : listUsers) {
			if (user.getId() == id) {
				user.setUser(id, username, firstName, lastName, dateOfBirth, address, phoneNumber, image, managerId, role);
				break;
			}
		}
	}
	
	public void clear(){
		instance = null ;
	}
}
